/**
 * 
 */
package com.csair.youth.web.listener;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.csair.youth.service.impl.CategoryServiceImpl;
import com.csair.youth.service.impl.PermissionServiceImpl;
import com.csair.youth.service.impl.SysConfigServiceImpl;
import com.csair.youth.service.impl.UserServiceImpl;

/**
 * @author mickey
 *
 */
public class ContextListener implements ServletContextListener {
	private static final Logger log = Logger.getLogger(ContextListener.class);
	/* (non-Javadoc)
	 * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
	 */
	public void contextDestroyed(ServletContextEvent sce) {
		// TODO Auto-generated method stub
		//结束时...
	}

	/* (non-Javadoc)
	 * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
	 */
	public void contextInitialized(ServletContextEvent sce) {
		// TODO Auto-generated method stub
		
		//初始化资源库 sys_config
		log.warn("正在初始化资源库...");
		ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext());
		String contextPath = sce.getServletContext().getContextPath();
		Map param = new HashMap();
		param.put("ContextPath", contextPath);
		SysConfigServiceImpl scsrv = (SysConfigServiceImpl)ctx.getBean("sysConfigService");
		scsrv.initGlobalSysConfigs(param);
		
		//初始化特殊权限
		log.warn("正在初始化特殊权限...");
		PermissionServiceImpl psrv = (PermissionServiceImpl)ctx.getBean("permissionService");
		psrv.initGlobalPermissions();
		
		//初始化特殊用户
		log.warn("正在初始化特殊用户...");
		UserServiceImpl usrv = (UserServiceImpl)ctx.getBean("userService");
		usrv.initGlobalUsers();
		
		//初始化特殊资源分类 ROOT和ftp上传（未分类）
		log.warn("正在初始化特殊资源分类...");
		CategoryServiceImpl csrv = (CategoryServiceImpl)ctx.getBean("categoryService");
		csrv.initGlobalCategories();
	}
}
